package pms.qa.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.opensymphony.xwork2.ActionSupport;

import pms.qa.service.QaDetailService;
import pms.common.value.QaListValue;

public class QaDetailAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private QaListValue detailList;
	private QaDetailService qaDetailService;
	private int currentStatus;
	private String[] limitDateSub;
	private String[] confirmDateSub;
	private Date currentDate;
	private List<HashMap> monthList;
	private List<HashMap> dayList;
	private String bango;
	private String fileNameToDownload;
	private String direction;
	private File myFile; 
	private String upFileName;

	
	public QaListValue getDetailList() {
		return detailList;
	}
	public void setDetailList(QaListValue detailList) {
		this.detailList = detailList;
	}
	
	public QaDetailService getQaDetailService() {
		return qaDetailService;
	}
	public void setQaDetailService(QaDetailService qaDetailService) {
		this.qaDetailService = qaDetailService;
	}
	
	public int getCurrentStatus() {
		return currentStatus;
	}
	public void setCurrentStatus(int currentStatus) {
		this.currentStatus = currentStatus;
	}
	public Date getCurrentDate() {
		return currentDate;
	}
	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}
	
	public void setMyFileFileName(String fileName) {
		this.upFileName = fileName;
	}
	
	public void setdetailListData() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		
		detailList = new QaListValue();
		detailList = qaDetailService.getListdetailData(bango.trim());
		currentStatus = qaDetailService.getCurrentStatus();
		limitDateSub = qaDetailService.getLimitDateSub();
		confirmDateSub = qaDetailService.getConfirmDateSub();
		
	}
	// 緊急度
	@SuppressWarnings("unchecked")
	public List<HashMap> getEmergencyDegreeList() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		List<HashMap> l_array = new ArrayList();
		l_array = qaDetailService.getMaterArrayList("qa_emergency");
       return l_array;
    }
	
	// 問合先
	@SuppressWarnings("unchecked")
	public List<HashMap> getAskedArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return qaDetailService.getAskedArray();
    }
	// 状態
	@SuppressWarnings("unchecked")
	public List<HashMap> getStatusList() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		List<HashMap> l_array = new ArrayList();
		l_array = qaDetailService.getMaterArrayList("qa_status");
		if(currentStatus==0){
			l_array.remove(2);
		}else if(currentStatus==1){
			l_array.remove(0);
		}else if(currentStatus==2){
			l_array = l_array.subList(2,3);
		}
       return l_array;
    }
	
	public String execute() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException, ParseException {
		setdetailListData();
		getMdlist();
		setCurrentDate(new Date());

		return SUCCESS;
	}
	public String modify() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		
		Date parselimitDate = new Date(detailList.getLimitDateY()+"/"+detailList.getLimitDateM()+"/"+detailList.getLimitDateD());
		String limitDate = formatter.format(parselimitDate);
		
		Date parseconfirmDate;
		String confirmDate = "";
		
		if(!"".equals(detailList.getConfirmDateM())) {		
			parseconfirmDate = new Date(detailList.getConfirmDateY()+"/"+detailList.getConfirmDateM()+"/"+detailList.getConfirmDateD());
			confirmDate = formatter.format(parseconfirmDate);
		}
		
		String currentDate = formatter.format(new Date());
		
		if(upFileName == null){
			setUpFileName("");
		}
		//qaDetailService.updateListdetailData(myFile,upFileName,detailList.getStatus(),detailList.getEmergencyDegree(),detailList.getToIAWaSeSaKi(),detailList.getSelBanGo(),detailList.getNaiYo(),limitDate,currentDate,confirmDate,detailList.getConfirmer(),detailList.getBiko(),detailList.getBanGo());
		qaDetailService.updateListdetailData(myFile,upFileName,detailList,limitDate,currentDate,confirmDate);
		return SUCCESS;
	}
	
	public String download() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
		System.out.println("bango :"+bango);
		System.out.println("direction :"+direction);
		qaDetailService.getBlobData(bango,direction);
		System.out.println("qaDetailService.getBlobData(bango,direction)");
		setFileNameToDownload(qaDetailService.getFilename());
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public void getMdlist(){
		monthList = new ArrayList();
		dayList = new ArrayList();

		for(int ilength=0;ilength<31;ilength++){
			HashMap monthHashMap = new HashMap();
			HashMap dayHashMap = new HashMap();
			if(ilength<9) {
				monthHashMap.put("month", "0" + (ilength+1));
				monthList.add(monthHashMap);
				
				dayHashMap.put("day", "0" + (ilength+1));
				dayList.add(dayHashMap);
			}
			else {
				if (ilength<12){
					monthHashMap.put("month",String.valueOf(ilength+1));
					monthList.add(monthHashMap);
				}
				dayHashMap.put("day",String.valueOf(ilength+1));
				dayList.add(dayHashMap);
			}
		}
	}
	
	
	public InputStream getInputStream() throws Exception { 
		return qaDetailService.getIs();
	}

	public String getDownloadFileName() throws UnsupportedEncodingException {
		String downFileName = fileNameToDownload; 
		downFileName = new String(downFileName.getBytes(), "ISO8859-1");
		System.out.println("getDownloadFileName()");
		return downFileName; 
	}
	
	public String[] getConfirmDateSub() {
		return confirmDateSub;
	}
	public void setConfirmDateSub(String[] confirmDateSub) {
		this.confirmDateSub = confirmDateSub;
	}
	public String[] getLimitDateSub() {
		return limitDateSub;
	}
	public void setLimitDateSub(String[] limitDateSub) {
		this.limitDateSub = limitDateSub;
	}
	public List getDayList() {
		return dayList;
	}
	@SuppressWarnings("unchecked")
	public void setDayList(java.util.List dayList) {
		this.dayList = dayList;
	}
	public List getMonthList() {
		return monthList;
	}

	@SuppressWarnings("unchecked")
	public void setMonthList(java.util.List monthList) {
		this.monthList = monthList;
	}
	public String getBango() {
		return bango;
	}
	public void setBango(String bango) {
		this.bango = bango;
	}
	public String getDirection() {
		return direction;
	}
	public void setDirection(String direction) {
		this.direction = direction;
	}
	
	public String getFileNameToDownload() {
		return fileNameToDownload;
	}
	public void setFileNameToDownload(String fileNameToDownload) {
		this.fileNameToDownload = fileNameToDownload;
	}
	public File getMyFile() {
		return myFile;
	}
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}
	public String getUpFileName() {
		return upFileName;
	}
	public void setUpFileName(String upFileName) {
		this.upFileName = upFileName;
	}
}
